package com.tripwego.client.ui.pageVoyage;

import java.util.Date;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.user.datepicker.client.DateBox.DefaultFormat;
import com.smartgwt.client.util.SC;
import com.tripwego.client.model.EtapeModel;

public class FormEditEtapeWidget extends FormAbstract {

	private EtapeModel etapeModelInit;

	private EtapeModel etapeModel;

	/*
	 * Elements graphiques
	 */
	private VerticalPanel contentPanel = new VerticalPanel();

	private HorizontalPanel panelActions = new HorizontalPanel();

	private final Button btn_update = new Button("<nobr>Mettre \u00E0 jour</nobr>");

	private final Button btn_cancel = new Button("Annuler");
	private final FlexTable flexTable = new FlexTable();
	private final Label lblDateDeDpart = new Label("* Date d'arriv\u00E9e");
	private final DateBox dateBox_start = new DateBox();
	private final Label lblDateDarrive = new Label("* Date d\u00E9part");
	private final DateBox dateBox_end = new DateBox();
	private final Label lbl_etapeName = new Label("Modification");
	private final Label lblNewLabel = new Label("* Nom de l'\u00E9tape");
	private final TextBox textBox_name = new TextBox();

	private DialogBox dialogBox = new DialogBox();
	private final Label lbl_delete = new Label("Supprimer l'\u00E9tape");

	/**
	 * 
	 */
	public FormEditEtapeWidget() {
		super();

		dialogBox.removeStyleDependentName("gwt-DialogBox");
		dialogBox.removeStyleName("gwt-DialogBox");

		// dialogBox_addTrip.setAnimationEnabled(true);
		dialogBox.setModal(false);
		dialogBox.setGlassEnabled(true);
		dialogBox.setHeight("");

		dialogBox.setWidget(contentPanel);

		contentPanel.setStyleName("popupFormulaire");

		lbl_etapeName.setStyleName("lblAddEtapeHover");

		contentPanel.add(lbl_etapeName);

		contentPanel.add(flexTable);

		contentPanel.add(panelActions);

		flexTable.setWidth("100%");
		contentPanel.setCellWidth(flexTable, "1%");

		flexTable.setWidget(0, 0, lblNewLabel);

		flexTable.setWidget(1, 0, textBox_name);

		flexTable.setWidget(2, 0, lblDateDeDpart);

		flexTable.setWidget(2, 1, lblDateDarrive);
		dateBox_start.setFormat(new DefaultFormat(DateTimeFormat.getShortDateFormat()));

		flexTable.setWidget(3, 0, dateBox_start);
		dateBox_end.setFormat(new DefaultFormat(DateTimeFormat.getShortDateFormat()));

		flexTable.setWidget(3, 1, dateBox_end);
		flexTable.getFlexCellFormatter().setColSpan(1, 0, 2);
		contentPanel.setWidth("350px");

		lbl_delete.setStyleName("lbl_action");

		panelActions.setSpacing(10);
		panelActions.add(btn_update);
		panelActions.setCellWidth(btn_update, "1%");
		panelActions.add(btn_cancel);
		panelActions.setCellWidth(btn_cancel, "1%");
		panelActions.add(lbl_delete);
		panelActions.setCellVerticalAlignment(lbl_delete, HasVerticalAlignment.ALIGN_MIDDLE);

		btn_update.addClickHandler(this);
		btn_cancel.addClickHandler(this);
		lbl_delete.addClickHandler(this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.google.gwt.user.client.ui.Widget#asWidget()
	 */
	@Override
	public Widget asWidget() {
		return dialogBox;
	}

	@Override
	public void onClick(ClickEvent event) {
		Object sender = event.getSource();

		if (sender == btn_update) {

			boolean erreur = false;
			removeAllError();

			if (dateBox_start.getValue() == null || dateBox_start.getValue().toString().isEmpty()) {
				dateBox_start.setStyleDependentName("error", true);
				erreur = true;

			}
			if (dateBox_end.getValue() == null || dateBox_end.getValue().toString().isEmpty()) {
				dateBox_end.setStyleDependentName("error", true);
				erreur = true;

			}

			if (!erreur) {
				Date dateStart = dateBox_start.getValue();
				Date dateEnd = dateBox_end.getValue();

				if (etapeModel.isFirstStep()) {
					if (dateStart.compareTo(dateEnd) > 0) {
						dateBox_end.setStyleDependentName("error", true);
						SC.warn("La date de depart doit etre superieure a la date d'arrivee");
						erreur = true;
					}
				} else {

					if (dateStart.compareTo(dateEnd) >= 0) {
						dateBox_end.setStyleDependentName("error", true);
						SC.warn("La date de depart doit etre superieure a la date d'arrivee");
						erreur = true;
					}
				}
			}

			if (!erreur) {
				etapeModel.setName(textBox_name.getValue());
				etapeModel.setDateDeb(dateBox_start.getValue());
				etapeModel.setDateFin(dateBox_end.getValue());

				if (!etapeModelInit.equals(etapeModel)) {

					if (!(etapeModelInit.getDateDeb().equals(etapeModel.getDateDeb()) && etapeModelInit.getDateFin().equals(etapeModel.getDateFin()))) {
						etapeModel.initListDay(true, etapeModelInit.getListDay().size());

						if (etapeModelInit.getDateFin().after(etapeModel.getDateFin())) {
							// TODO
						}
						if (etapeModelInit.getDateFin().before(etapeModel.getDateFin())) {
							// TODO
						}
					}

					presenter.onUpdateEtape(etapeModel);
				}

				dialogBox.hide();
			}
		}
		if (sender == btn_cancel) {
			dialogBox.hide();
		}
		if (sender == lbl_delete) {
			SC.warn("todo");
		}
	}

	@Override
	public void init() {
		removeAllError();

		lbl_etapeName.setText(etapeModel.getName());
		textBox_name.setValue(etapeModel.getName());
		dateBox_start.setValue(etapeModel.getDateDeb());
		dateBox_end.setValue(etapeModel.getDateFin());

		dateBox_start.setEnabled(false);

		show();
	}

	/**
	 * suppression des erreurs
	 */
	public void removeAllError() {
		dateBox_start.setStyleDependentName("error", false);
		dateBox_end.setStyleDependentName("error", false);
	}

	/**
	 * @return the etapeModel
	 */
	public EtapeModel getEtapeModel() {
		return etapeModel;
	}

	/**
	 * @param etapeModel
	 *            the etapeModel to set
	 * @throws CloneNotSupportedException
	 */
	public void setEtapeModel(EtapeModel etapeModel) {
		this.etapeModel = etapeModel;
		this.etapeModelInit = new EtapeModel();
		etapeModelInit.cloner(etapeModel);
	}
}
